/*
 * UserNet  --  TCP/IP implementation in userspace
 * Copyright (C) 2010  Robert Millan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#include "unet/ipc.h"

#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>

struct targ
{
  int fd;
};

static void *
loop (void *arg)
{
  struct targ *a = arg;

  printf ("I'm a new thread\n");

  while (1)
    {
      char msg[256];

      if (read (a->fd, msg, sizeof (msg)) > 0)
      	printf ("received from client: %s\n", msg);
    }

  return NULL;
}


int
main (int argc, char *argv[])
{
  int listenfd = unet_ipc_listen ();

  while (1)
    {
      pthread_t thread;

      struct targ *a = malloc (sizeof (*a));
      a->fd = unet_ipc_accept (listenfd);

      printf ("spawning thread\n");

      pthread_create (&thread,
      		      NULL /* default attributes */,
		      loop, a);
    }

  return 0;
}
